package com.hsj.xiaokang.comparableandcomparator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 1.
 * 　Comparable是排序接口。
 * 若一个类实现了Comparable接口，就意味着该类支持排序。实现了Comparable接口的类的对象的列表或数组可以通过Collections.sort或Arrays.sort进行自动排序。
 * 
 * 
 * @Description:TODO
 * @author:hsj qq:2356899074
 * @time:2017年11月8日 上午11:26:49
 */
public class Test_1 {
   
	private final static Logger LOGGR = LoggerFactory.getLogger(Test_1.class);
	
	 public static void main(String[] args)
	    {
//	        Person[] people=new Person[]{new Person("xujian", 20),new Person("xiewei", 10)};
		    List<Person> people = new ArrayList<>();
		    people.add(new Person("xujian", 20));
		    people.add(new Person("xiewei", 10));
	        LOGGR.info("排序前");
	        
	        for (Person person : people)
	        {
	            LOGGR.info(person.toString());
	        }
//	        Arrays.sort(people);
	        Collections.sort(people);
	        LOGGR.info("\n排序后");
	        for (Person person : people)
	        {
	        	LOGGR.info(person.toString());
	        }
	    }
	
	public static  class Person implements Comparable<Person>
	{
	    String name;
	    int age;
	    
	    public Person(String name, int age)
	    {
	        super();
	        this.name = name;
	        this.age = age;
	    }
	    public String getName()
	    {
	        return name;
	    }
	    public int getAge()
	    {
	        return age;
	    }
	    @Override
	    public int compareTo(Person p)
	    {
	        return this.age-p.getAge();
	    }
		@Override
		public String toString() {
			return "Person [name=" + name + ", age=" + age + "]";
		}
	    
	   
	}
}
